# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

class BiliPipeline(object):
    def open_spider(self,spider):
        self.coon=pymysql.connect('127.0.0.1','root','123456','bili',charset='utf8')
        self.cursor = self.coon.cursor()
    def process_item(self, item, spider):
        try:
            sql,data = item.save_to_mysql()
            self.cursor.execute(sql,data)
            self.coon.commit()
            print('写入成功OK')
        except Exception as e:
            print('写入失败,原因%s'%e)
        return item
    def close_spider(self, spider):
        self.cursor.close()
        self.coon.close()

# class InsertRedis(object):
#     def __init__(self):
#         self.r = redis.Redis(host='127.0.0.1', port=6379,decode_responses=True)
#     def process_item(self,item,spider):
#         self.r.write(item['url'])